<template>
	<view class="scrool-page">
		<view class="list-cont">
			<scroll-view  class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in productList" :key="item.id">
					<uni-card :title="'产成品序号:'+(index+1)" v-if='!item.flag'>
						<text class="uni-body">物料编码：{{ item.code }}</text><br>
						<text class="uni-body">数量：{{ item.codeEqualQty }}</text><br>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
					
					<uni-card :title="'产成品序号:'+(index+1)" v-else style='background-color: darkgrey;'>
						<text class="uni-body">物料编码：{{ item.code }}</text><br>
						<text class="uni-body">数量：{{ item.codeEqualQty }}</text><br>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
			</scroll-view>
		</view>
		
		<view class="footer">
			<button type="default" size='mini' @click="reduceData" style="width:48%; margin-left:1%;" v-if="!userflag">减码</button>
			<button type="default" size='mini' @click="reduceData" style="width:48%; margin-left:1%;" v-else>加箱</button>
			<button type="default" size='mini' @click="submit" style="width:49%; margin-left:1%;">完成</button>
		</view>
		
		<uni-popup ref="alertDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true" :content="content" @confirm="dialogConfirm"
				@close="dialogClose" >
			</uni-popup-dialog>
		</uni-popup>
		
		<uni-popup ref="reduceDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true" :content="reduceContent" @confirm="reduceConfirm"
				@close="reduceClose">
			</uni-popup-dialog>
		</uni-popup>
		
		<uni-popup ref="messageDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true" :content="messageeContent" @confirm="messageConfirm"
				@close="messageClose">
			</uni-popup-dialog>
		</uni-popup>
		
		<uni-popup ref="dataDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true" :content="dataContent" @confirm="dataConfirm"
				@close="dataClose">
			</uni-popup-dialog>
		</uni-popup>
	</view>	
</template>

<script>
	import { getToken } from '@/utils/auth';
	import common from '../../pages/common/common.js'
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				productList:[],
				content:'',
				code:'',
				codeEqualQty:'',
				boxCode:'',
				count:0,
				userflag:false,
				codeStr:'',
				popFlag:false,
				reduceContent:'',
				reduceCode:'',
				reduceNumkber:'',
				reducePatch:'',
				reduceCount:0,
				barcodeIdList:[],
				dataContent:'检测到有缓存数据是否加载？',
				messageeContent:'确认删除数据吗?',
				sumbitFlag : false,
				baseUrl:getApp().globalData.config.baseUrl,
				userName:this.$store.state.user.name,
				orgId:getApp().globalData.config.orgId,
				storageLocation:'',
			}
		},
				
		created: function() {
			this.orgId = uni.getStorageSync('orgId');
			this.content = '';
			this.code = '';
			this.codeEqualQty = '';
			this.boxCode = '';
			this.count = 0;
			this.userflag = false;
			this.codeStr = '';
			this.popFlag = false;
			this.reduceContent = '';
			this.reduceCode = '';
			this.reduceNumkber = '';
			this.reducePatch = '';
			this.reduceCount = 0;
			this.barcodeIdList = [];
		},
		
		onShow() {
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},
		
		onHide: function() {
			this.unregisterScan();
		},
		
		destroyed: function() {
			this.unregisterScan();
		},
		
		onLoad: function(option){
			this.orgId = uni.getStorageSync('orgId');
			this.storageLocation = option.storageLocation;
			uni.setNavigationBarTitle({
				title:this.storageLocation+'货位重新扫箱码'
			})
		},
		
		watch: {
			popFlag:{
				handler(newValue, oldValue){
					if(newValue){
						this.$refs.alertDialog.open();
						this.content = '物料编码：' +this.code+"\n"+"数量："+this.codeEqualQty+"\n"+'箱码：'+this.boxCode+'\n';
					}
				}
			}
			
		},
		
		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
			     return false;
			}
			//that.unregisterScan();
			uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
			    delta: 1
			});
			return true;
		},
		
		methods: {
			initScan(popFlag) {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
					    let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描
						_this.buttonScan(codeStr);
						_this.viewScan(codeStr);												
					}
				});				
			},
			
			reduceData(){
				if(this.userflag){
					this.userflag = false;
				}else{
					this.userflag = true;
				}
				//this.$refs.messageDialog.open();			
			},
			
			//如果是机器上面的扫描按钮，是增加数据
			buttonScan(codeStr){
				if(this.userflag){
					return;
				}
				if(codeStr == null || codeStr == ''){
					uni.showToast({
						title: '请扫描条码',
						icon: 'error',
						duration: 2000
					});
					return;
				}
				if(!this.popFlag){
					this.popFlag = true;
				}
				var result = common.scanCode(codeStr);
				if(result[0] == "P"){
					this.code = result[1];
					this.count = parseInt(this.count)+1;
					this.content = '物料编码：' +this.code+"\n"+"数量："+this.codeEqualQty+"\n"+'箱码：'+this.boxCode+'\n';
				}else if(result[0]== 'Q'){
					this.codeEqualQty = result[1];	
					this.count = parseInt(this.count)+1;
					this.content = '物料编码：' +this.code+"\n"+"数量："+this.codeEqualQty+"\n"+'箱码：'+this.boxCode+'\n';
				}else if(result[0]== 'C'){
					for(var i=0;i<this.productList.length;i++){
						if(this.productList[i].boxCode == result[1]){
							uni.showToast({
								title: '此数据已存在',
								icon: 'error',
								duration: 2000
							});
							return;
						}
					}
					this.boxCode = result[1];
					this.count = parseInt(this.count)+1;
					this.content = '物料编码：' +this.code+"\n"+"数量："+this.codeEqualQty+"\n"+'箱码：'+this.boxCode+'\n';
				}else{
					uni.showToast({
						title: '请正确扫描条码',
						icon: 'error',
						duration: 2000
					});
					return;
				}
				
			},
			
			//如果是页面上的减码扫描，那就是删除数据
			viewScan(codeStr){
				if(!this.userflag){
					return;
				}
				var result = common.scanCode(codeStr);
				if(result[0] == "C"){
					this.reducePatch = result[1];
					this.reduceContent = '箱码：'+this.reducePatch+'\n';
				}else{
					uni.showToast({
						title: '请扫描箱码',
						icon: 'error',
						duration: 5000
					});
					this.userflag = false;
				}
				this.$refs.reduceDialog.open();
			},
			
			submit(){
				var that = this;
				let pages = getCurrentPages();  //获取所有页面栈实例列表
				let nowPage = pages[ pages.length - 1];  //当前页页面实例
				let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
				for(var i=0;i<prevPage.$vm.productList.length;i++){
					if(prevPage.$vm.productList[i].storageLocation == that.storageLocation){
						prevPage.$vm.productList[i].flag = true;
						break;
					}
				}
				//that.unregisterScan();
				setTimeout(function(){
					uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
						delta: 1
					});
				},1000);	
			},
			
			registerScan() {
				main.registerReceiver(receiver, filter);
			},
			
			unregisterScan() {
				main.unregisterReceiver(receiver);
			},
			
			dialogConfirm(){
				//如果三个码都扫描完成
				var that = this;
				if(that.code == null || that.code == ''){
					uni.showToast({
						title: '请扫描物料编码',
						icon: 'error',
						duration: 2000
					});
					return;
				}else if(that.codeEqualQty == null || that.codeEqualQty == ''){
					uni.showToast({
						title: '请扫描数量',
						icon: 'error',
						duration: 2000
					});
					return;
				}else if(that.boxCode == null || that.boxCode == ''){
					uni.showToast({
						title: '请扫描箱码',
						icon: 'error',
						duration: 2000
					});
					return;
				}else{
					var tempList = {id:that.productList.length+1,code:that.code,codeEqualQty:that.codeEqualQty,boxCode:that.boxCode,flag:false};
					that.productList.push(tempList);		
					that.$refs.alertDialog.close();
				}
			},
			
			dialogClose(){				
				this.code = '';
				this.codeEqualQty='';
				this.boxCode ='';
				this.count = '0';
				this.popFlag = false;
				this.$refs.alertDialog.close();
			},
			
			reduceConfirm(){
				//if(this.reduceCount == 3){
					var tempList = [];
					for(var i=0;i<this.productList.length;i++){
						if(this.productList[i].boxCode == this.reducePatch){
							
						}else{
							tempList.push(this.productList[i]);
						}
					}
					this.productList = [];
					this.productList = tempList;
					this.$refs.reduceDialog.close();
					this.reduceCode = '';
					this.reduceNumkber='';
					this.reducePatch ='';
					this.reduceCount = 0;
					this.userflag = false;
				// }else{
				// 	uni.showToast({
				// 		title: '请正确扫描物料信息',
				// 		icon: 'error',
				// 		duration: 2000
				// 	});
				// 	return;
				// }
				
			},
			
			reduceClose(){
				this.reduceCode = '';
				this.reduceNumkber='';
				this.reducePatch ='';
				this.reduceCount = 0;
				this.userflag = false;
				this.$refs.reduceDialog.close();
			},
			
			dataConfirm(){
				this.productList = this.list;
				this.$refs.dataDialog.close();
			},
			
			dataClose(){
				this.list = [];
				try {
					uni.removeStorageSync('product_recpt');
				} catch (e) {
					console.log(e.message);
				}
				this.productList = [];
				this.$refs.dataDialog.close();
			},
			
			messageConfirm(){
				scanDevice.stopScan(); // 停止扫描
				scanDevice.startScan();
				this.$refs.messageDialog.close();
			},
			
			messageClose(){
				this.userflag = false;
				this.$refs.messageDialog.close();
			},
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}
	.text-content {
		font-size: 48rpx;
		text-align: center;
	}
	
	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}
	
	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height:30px;
		font-size: 26rpx;
	}
	page {
		width: 100%;
		height: 100%;
	}
	
	
	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}
	
	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}
	
	.list-cont {
		flex: 1;
		position: relative;
	
		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}
	
		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;
	
		}
	}
	
	
	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>
